<template>
  <div class="config-page">
    <h4 class="zl-title">配置集列表<Button type="primary" @click="configAddModify()" class="fr"><Icon type="md-add" /> 添加配置集</Button></h4>
    <div class="zl-page-loading" v-if="isLoading">
      <Spin fix></Spin>
    </div>
    <Table v-else :columns="columns" :data="configList" class="config-table"></Table>

    <!-- 新增、修改 -->
    <add-modify-model :visible.sync="configVisible" :form="configForm" @finish="init" ></add-modify-model>
    <!-- 详情 -->
    <detail-model :visible.sync="detailVisible" :form="configForm"></detail-model>
  </div>
</template>
<script type="text/javascript">
import Fetch from '@/api/request'
import AddModifyModel from '@/components/config-set/add-modify-model'
import DetailModel from '@/components/config-set/detail-model'
export default {
  components: { AddModifyModel, DetailModel },
  data() {
    return {
      columns: [{
        type: 'index',
        width: 60
      }, {
        title: '名称',
        key: 'id'
      }, {
        title: '创建时间',
        key: 'createTime'
      }, {
        title: '操作',
        render: (h, params) => {
          return h('div', [
            h('div', {
              class: 'button-inner',
              on: {
                click: () => {
                  this.configDetail(params.row)
                }
              }
            }, [
              h('Icon', {
                props: {
                  type: 'ios-paper'
                }
              }, ''),
              '详情'
            ]),
            h('div', {
              class: 'button-inner',
              on: {
                click: () => {
                  this.configAddModify(params.row)
                }
              }
            }, [
              h('Icon', {
                props: {
                  type: 'ios-create-outline'
                }
              }, ''),
              '编辑'
            ]),
            h('div', {
              class: 'button-inner',
              on: {
                click: () => {
                  this.configDelete(params.row)
                }
              }
            }, [
              h('Icon', {
                props: {
                  type: 'md-trash'
                }
              }, ''),
              '删除'
            ])
          ])
        }
      }],
      configList: [],
      configForm: {},
      configVisible: false,
      detailVisible: false,
      isLoading: true
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 删除配置集
    configDelete(config) {
      this.$Modal.confirm({
        title: '系统提示',
        content: '确定删除配置集（' + config.id + '）?',
        loading: true,
        onOk: () => {
          Fetch.delete({
            url: '/api/environments/' + this.$route.params.environmentId + '/configmaps/' + config.id
          }).then((res) => {
            if (res && res.code === '0') {
              this.$Message.success({
                content: '删除配置集（' + config.id + '）成功！',
                duration: this.DURATION
              })
              this.init()
            } else {
              this.$Message.warning({
                content: res.message || '服务器内部错误',
                duration: this.DURATION
              })
            }
            this.$Modal.remove()
          }).catch((err) => {
            this.$Modal.remove()
            this.$Message.error(err.message)
          })
        }
      })
    },
    // 配置集详情
    configDetail(config) {
      this.configForm = Object.assign({}, config)
      this.detailVisible = true
    },
    // 配置集新增、修改
    configAddModify(config) {
      if (config) {
        this.configForm = Object.assign({environmentName: config.environmentId}, config)
      } else {
        this.configForm = {
          environmentName: '',
          id: ''
        }
      }
      this.configVisible = true
    },
    // 获取配置集初始数据
    init() {
      Fetch.get({
        url: '/api/environments/' + this.$route.params.environmentId + '/configmaps'
      }).then((res) => {
        if (res && res.code === '0') {
          this.configList = res.data
        } else {
          this.$Message.warning({
            content: res.message || '服务器内部错误',
            duration: this.DURATION
          })
        }
        this.isLoading = false
      }).catch((err) => {
        this.isLoading = false
        this.$Message.error(err.message)
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.config-page {
  padding: 20px;
  .config-table {
    margin-top: 20px;
  }
  .zl-title {
    margin-top: 10px;
  }
}
</style>
